package androidx.core.provider;

import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.content.pm.Signature;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.net.Uri.Builder;
import android.os.Build.VERSION;
import android.os.CancellationSignal;
import androidx.annotation.GuardedBy;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.res.FontResourcesParserCompat;
import com.huawei.cloudservice.CloudAccountManager;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import o.dx;
import o.dy;
import o.ft;
import o.gh;
import o.go;
import o.gw;
import o.gw.a;
import o.hf;

public class FontsContractCompat {
    static final dx<String, Typeface> ˊ = new dx(16);
    @GuardedBy("sLock")
    static final dy<String, ArrayList<a<e>>> ˋ = new dy();
    private static final gw ˎ = new gw("fonts", 10, 10000);
    static final Object ˏ = new Object();
    private static final Comparator<byte[]> ॱ = new Comparator<byte[]>() {
        public /* synthetic */ int compare(Object obj, Object obj2) {
            return ॱ((byte[]) obj, (byte[]) obj2);
        }

        public int ॱ(byte[] bArr, byte[] bArr2) {
            if (bArr.length != bArr2.length) {
                return bArr.length - bArr2.length;
            }
            for (int i = 0; i < bArr.length; i++) {
                if (bArr[i] != bArr2[i]) {
                    return bArr[i] - bArr2[i];
                }
            }
            return 0;
        }
    };

    public static class b {
        private final Uri ˊ;
        private final int ˋ;
        private final int ˎ;
        private final int ˏ;
        private final boolean ॱ;

        @RestrictTo({androidx.annotation.RestrictTo.d.ॱ})
        public b(@NonNull Uri uri, @IntRange(from = 0) int i, @IntRange(from = 1, to = 1000) int i2, boolean z, int i3) {
            this.ˊ = (Uri) hf.ˊ(uri);
            this.ˋ = i;
            this.ˎ = i2;
            this.ॱ = z;
            this.ˏ = i3;
        }

        @NonNull
        public Uri ˊ() {
            return this.ˊ;
        }

        @IntRange(from = 0)
        public int ˎ() {
            return this.ˋ;
        }

        @IntRange(from = 1, to = 1000)
        public int ˏ() {
            return this.ˎ;
        }

        public boolean ˋ() {
            return this.ॱ;
        }

        public int ॱ() {
            return this.ˏ;
        }
    }

    public static class d {
        private final int ˋ;
        private final b[] ˎ;

        @RestrictTo({androidx.annotation.RestrictTo.d.ॱ})
        public d(int i, @Nullable b[] bVarArr) {
            this.ˋ = i;
            this.ˎ = bVarArr;
        }

        public int ˋ() {
            return this.ˋ;
        }

        public b[] ˎ() {
            return this.ˎ;
        }
    }

    static final class e {
        final Typeface ˎ;
        final int ॱ;

        e(@Nullable Typeface typeface, int i) {
            this.ˎ = typeface;
            this.ॱ = i;
        }
    }

    @NonNull
    static e ˎ(Context context, go goVar, int i) {
        int i2 = -3;
        try {
            d ˋ = ˋ(context, null, goVar);
            if (ˋ.ˋ() == 0) {
                Typeface ˎ = ft.ˎ(context, null, ˋ.ˎ(), i);
                if (ˎ != null) {
                    i2 = 0;
                }
                return new e(ˎ, i2);
            }
            if (ˋ.ˋ() == 1) {
                i2 = -2;
            }
            return new e(null, i2);
        } catch (NameNotFoundException e) {
            return new e(null, -1);
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    @androidx.annotation.RestrictTo({androidx.annotation.RestrictTo.d.ॱ})
    public static android.graphics.Typeface ˏ(final android.content.Context r6, final o.go r7, @androidx.annotation.Nullable final o.fv.d r8, @androidx.annotation.Nullable final android.os.Handler r9, boolean r10, int r11, final int r12) {
        /*
        r2 = 0;
        r0 = new java.lang.StringBuilder;
        r0.<init>();
        r1 = r7.ʻ();
        r0 = r0.append(r1);
        r1 = "-";
        r0 = r0.append(r1);
        r0 = r0.append(r12);
        r3 = r0.toString();
        r0 = ˊ;
        r0 = r0.ˏ(r3);
        r0 = (android.graphics.Typeface) r0;
        if (r0 == 0) goto L_0x002d;
    L_0x0026:
        if (r8 == 0) goto L_0x002b;
    L_0x0028:
        r8.ˎ(r0);
    L_0x002b:
        r2 = r0;
    L_0x002c:
        return r2;
    L_0x002d:
        if (r10 == 0) goto L_0x004a;
    L_0x002f:
        r0 = -1;
        if (r11 != r0) goto L_0x004a;
    L_0x0032:
        r0 = ˎ(r6, r7, r12);
        if (r8 == 0) goto L_0x0041;
    L_0x0038:
        r1 = r0.ॱ;
        if (r1 != 0) goto L_0x0044;
    L_0x003c:
        r1 = r0.ˎ;
        r8.ˋ(r1, r9);
    L_0x0041:
        r2 = r0.ˎ;
        goto L_0x002c;
    L_0x0044:
        r1 = r0.ॱ;
        r8.ˊ(r1, r9);
        goto L_0x0041;
    L_0x004a:
        r4 = new androidx.core.provider.FontsContractCompat$3;
        r4.<init>(r6, r7, r12, r3);
        if (r10 == 0) goto L_0x005c;
    L_0x0051:
        r0 = ˎ;	 Catch:{ InterruptedException -> 0x0098 }
        r0 = r0.ॱ(r4, r11);	 Catch:{ InterruptedException -> 0x0098 }
        r0 = (androidx.core.provider.FontsContractCompat.e) r0;	 Catch:{ InterruptedException -> 0x0098 }
        r2 = r0.ˎ;	 Catch:{ InterruptedException -> 0x0098 }
        goto L_0x002c;
    L_0x005c:
        if (r8 != 0) goto L_0x0076;
    L_0x005e:
        r1 = r2;
    L_0x005f:
        r5 = ˏ;
        monitor-enter(r5);
        r0 = ˋ;	 Catch:{ all -> 0x0073 }
        r0 = r0.get(r3);	 Catch:{ all -> 0x0073 }
        r0 = (java.util.ArrayList) r0;	 Catch:{ all -> 0x0073 }
        if (r0 == 0) goto L_0x007d;
    L_0x006c:
        if (r1 == 0) goto L_0x0071;
    L_0x006e:
        r0.add(r1);	 Catch:{ all -> 0x0073 }
    L_0x0071:
        monitor-exit(r5);	 Catch:{ all -> 0x0073 }
        goto L_0x002c;
    L_0x0073:
        r0 = move-exception;
        monitor-exit(r5);	 Catch:{ all -> 0x0073 }
        throw r0;
    L_0x0076:
        r0 = new androidx.core.provider.FontsContractCompat$5;
        r0.<init>(r8, r9);
        r1 = r0;
        goto L_0x005f;
    L_0x007d:
        if (r1 == 0) goto L_0x008c;
    L_0x007f:
        r0 = new java.util.ArrayList;	 Catch:{ all -> 0x0073 }
        r0.<init>();	 Catch:{ all -> 0x0073 }
        r0.add(r1);	 Catch:{ all -> 0x0073 }
        r1 = ˋ;	 Catch:{ all -> 0x0073 }
        r1.put(r3, r0);	 Catch:{ all -> 0x0073 }
    L_0x008c:
        monitor-exit(r5);	 Catch:{ all -> 0x0073 }
        r0 = ˎ;
        r1 = new androidx.core.provider.FontsContractCompat$1;
        r1.<init>(r3);
        r0.ॱ(r4, r1);
        goto L_0x002c;
    L_0x0098:
        r0 = move-exception;
        goto L_0x002c;
        */
        throw new UnsupportedOperationException("Method not decompiled: androidx.core.provider.FontsContractCompat.ˏ(android.content.Context, o.go, o.fv$d, android.os.Handler, boolean, int, int):android.graphics.Typeface");
    }

    @RequiresApi(19)
    @RestrictTo({androidx.annotation.RestrictTo.d.ॱ})
    public static Map<Uri, ByteBuffer> ˋ(Context context, b[] bVarArr, CancellationSignal cancellationSignal) {
        Map hashMap = new HashMap();
        for (b bVar : bVarArr) {
            if (bVar.ॱ() == 0) {
                Uri ˊ = bVar.ˊ();
                if (!hashMap.containsKey(ˊ)) {
                    hashMap.put(ˊ, gh.ˋ(context, cancellationSignal, ˊ));
                }
            }
        }
        return Collections.unmodifiableMap(hashMap);
    }

    @NonNull
    public static d ˋ(@NonNull Context context, @Nullable CancellationSignal cancellationSignal, @NonNull go goVar) throws NameNotFoundException {
        ProviderInfo ॱ = ॱ(context.getPackageManager(), goVar, context.getResources());
        if (ॱ == null) {
            return new d(1, null);
        }
        return new d(0, ˎ(context, goVar, ॱ.authority, cancellationSignal));
    }

    @Nullable
    @RestrictTo({androidx.annotation.RestrictTo.d.ॱ})
    @VisibleForTesting
    public static ProviderInfo ॱ(@NonNull PackageManager packageManager, @NonNull go goVar, @Nullable Resources resources) throws NameNotFoundException {
        String ˏ = goVar.ˏ();
        ProviderInfo resolveContentProvider = packageManager.resolveContentProvider(ˏ, 0);
        if (resolveContentProvider == null) {
            throw new NameNotFoundException("No package found for authority: " + ˏ);
        } else if (resolveContentProvider.packageName.equals(goVar.ˊ())) {
            List ॱ = ॱ(packageManager.getPackageInfo(resolveContentProvider.packageName, 64).signatures);
            Collections.sort(ॱ, ॱ);
            List ॱ2 = ॱ(goVar, resources);
            for (int i = 0; i < ॱ2.size(); i++) {
                List arrayList = new ArrayList((Collection) ॱ2.get(i));
                Collections.sort(arrayList, ॱ);
                if (ˏ(ॱ, arrayList)) {
                    return resolveContentProvider;
                }
            }
            return null;
        } else {
            throw new NameNotFoundException("Found content provider " + ˏ + ", but package was not " + goVar.ˊ());
        }
    }

    private static List<List<byte[]>> ॱ(go goVar, Resources resources) {
        if (goVar.ˋ() != null) {
            return goVar.ˋ();
        }
        return FontResourcesParserCompat.ˋ(resources, goVar.ˎ());
    }

    private static boolean ˏ(List<byte[]> list, List<byte[]> list2) {
        if (list.size() != list2.size()) {
            return false;
        }
        for (int i = 0; i < list.size(); i++) {
            if (!Arrays.equals((byte[]) list.get(i), (byte[]) list2.get(i))) {
                return false;
            }
        }
        return true;
    }

    private static List<byte[]> ॱ(Signature[] signatureArr) {
        List<byte[]> arrayList = new ArrayList();
        for (Signature toByteArray : signatureArr) {
            arrayList.add(toByteArray.toByteArray());
        }
        return arrayList;
    }

    @VisibleForTesting
    @NonNull
    static b[] ˎ(Context context, go goVar, String str, CancellationSignal cancellationSignal) {
        Throwable th;
        Cursor cursor;
        ArrayList arrayList = new ArrayList();
        Uri build = new Builder().scheme("content").authority(str).build();
        Uri build2 = new Builder().scheme("content").authority(str).appendPath("file").build();
        try {
            Cursor query;
            ArrayList arrayList2;
            if (VERSION.SDK_INT > 16) {
                query = context.getContentResolver().query(build, new String[]{"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", CloudAccountManager.KEY_RESULT_CODE}, "query = ?", new String[]{goVar.ॱ()}, null, cancellationSignal);
            } else {
                query = context.getContentResolver().query(build, new String[]{"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", CloudAccountManager.KEY_RESULT_CODE}, "query = ?", new String[]{goVar.ॱ()}, null);
            }
            if (query != null) {
                try {
                    if (query.getCount() > 0) {
                        int columnIndex = query.getColumnIndex(CloudAccountManager.KEY_RESULT_CODE);
                        arrayList2 = new ArrayList();
                        int columnIndex2 = query.getColumnIndex("_id");
                        int columnIndex3 = query.getColumnIndex("file_id");
                        int columnIndex4 = query.getColumnIndex("font_ttc_index");
                        int columnIndex5 = query.getColumnIndex("font_weight");
                        int columnIndex6 = query.getColumnIndex("font_italic");
                        while (query.moveToNext()) {
                            Uri withAppendedId;
                            int i = columnIndex != -1 ? query.getInt(columnIndex) : 0;
                            int i2 = columnIndex4 != -1 ? query.getInt(columnIndex4) : 0;
                            if (columnIndex3 == -1) {
                                withAppendedId = ContentUris.withAppendedId(build, query.getLong(columnIndex2));
                            } else {
                                withAppendedId = ContentUris.withAppendedId(build2, query.getLong(columnIndex3));
                            }
                            int i3 = columnIndex5 != -1 ? query.getInt(columnIndex5) : 400;
                            boolean z = columnIndex6 != -1 && query.getInt(columnIndex6) == 1;
                            arrayList2.add(new b(withAppendedId, i2, i3, z, i));
                        }
                        if (query != null) {
                            query.close();
                        }
                        return (b[]) arrayList2.toArray(new b[0]);
                    }
                } catch (Throwable th2) {
                    th = th2;
                    cursor = query;
                }
            }
            arrayList2 = arrayList;
            if (query != null) {
                query.close();
            }
            return (b[]) arrayList2.toArray(new b[0]);
        } catch (Throwable th3) {
            th = th3;
            cursor = null;
            if (cursor != null) {
                cursor.close();
            }
            throw th;
        }
    }
}
